package cn.com.guage.dtm.verify.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.guage.dtm.common.annotation.Log;
import cn.com.guage.dtm.common.enums.BusinessType;
import cn.com.guage.dtm.verify.domain.VerifyFieldRuleManage;
import cn.com.guage.dtm.verify.domain.VerifyTaskManage;
import cn.com.guage.dtm.verify.service.IVerifyFieldRuleManageService;
import cn.com.guage.dtm.verify.service.IVerifyTaskManageService;
import cn.com.guage.dtm.common.core.controller.BaseController;
import cn.com.guage.dtm.common.core.domain.AjaxResult;
import cn.com.guage.dtm.common.utils.poi.ExcelUtil;
import cn.com.guage.dtm.metadata.domain.MdSys;
import cn.com.guage.dtm.metadata.service.IMdSysService;
import cn.com.guage.dtm.common.core.page.TableDataInfo;
import cn.com.guage.dtm.common.core.text.Convert;

/**
 * 检核作业管理Controller
 * 
 * @author yangdechao
 * @date 2021-06-24
 */
@Controller
@RequestMapping("/verify/task")
public class VerifyTaskManageController extends BaseController {
	private String prefix = "verify/task";

	@Autowired
	private IVerifyTaskManageService verifyTaskManageService;

	@Autowired
	private IMdSysService mdSysService;

	@Autowired
	private IVerifyFieldRuleManageService verifyFieldRuleManageService;

	@RequiresPermissions("verify:task:view")
	@GetMapping()
	public String task() {
		return prefix + "/task";
	}

	/**
	 * 查询检核作业管理列表
	 */
	@RequiresPermissions("verify:task:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(VerifyTaskManage verifyTaskManage) {
		startPage();
		List<VerifyTaskManage> list = verifyTaskManageService.selectVerifyTaskManageList(verifyTaskManage);
		return getDataTable(list);
	}

	/**
	 * 导出检核作业管理列表
	 */
	@RequiresPermissions("verify:task:export")
	@Log(title = "检核作业管理", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(VerifyTaskManage verifyTaskManage) {
		List<VerifyTaskManage> list = verifyTaskManageService.selectVerifyTaskManageList(verifyTaskManage);
		ExcelUtil<VerifyTaskManage> util = new ExcelUtil<VerifyTaskManage>(VerifyTaskManage.class);
		return util.exportExcel(list, "检核作业管理数据");
	}

	/**
	 * 新增检核作业管理
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap) {
		long timeNew = System.currentTimeMillis() / 1000; // 10位数的时间戳
		String taskCode = getNewEquipmentNo("CHECK_JOB", String.valueOf(timeNew));
		List<MdSys> mdSysList = mdSysService.selectMdSysList(null);
		mmap.put("taskCode", taskCode);
		mmap.put("mdSysList", mdSysList);
		mmap.put("className", "TaskClass");
		mmap.put("packageName", "cn.com.guage.dtm.check");
		return prefix + "/add";
	}

	/**
	 * 新增保存检核作业管理
	 */
	@RequiresPermissions("verify:task:add")
	@Log(title = "检核作业管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(VerifyTaskManage verifyTaskManage) {
		return toAjax(verifyTaskManageService.insertVerifyTaskManage(verifyTaskManage));
	}

	/**
	 * 修改检核作业管理
	 */
	@GetMapping("/edit/{taskId}")
	public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap) {
		VerifyTaskManage verifyTaskManage = verifyTaskManageService.selectVerifyTaskManageById(taskId);
		mmap.put("verifyTaskManage", verifyTaskManage);
		List<MdSys> mdSysList = mdSysService.selectMdSysList(null);
		mmap.put("mdSysList", mdSysList);
		return prefix + "/edit";
	}

	/**
	 * 修改保存检核作业管理
	 */
	@RequiresPermissions("verify:task:edit")
	@Log(title = "检核作业管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(VerifyTaskManage verifyTaskManage) {
		return toAjax(verifyTaskManageService.updateVerifyTaskManage(verifyTaskManage));
	}

	/**
	 * 删除检核作业管理
	 */
	@RequiresPermissions("verify:task:remove")
	@Log(title = "检核作业管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(verifyTaskManageService.deleteVerifyTaskManageByIds(ids));
	}

	/**
	 * 任务类型+五位编号（从1开始，不够前补0）
	 * 
	 * @param equipmentType 设备类型
	 * @param equipmentNo   最新设备编号
	 * @return
	 */
	public static String getNewEquipmentNo(String equipmentType, String equipmentNo) {
		String newEquipmentNo = equipmentType + "00001";

		if (equipmentNo != null && !equipmentNo.isEmpty()) {
			int no = Integer.parseInt(equipmentNo);
			int newEquipment = ++no;
			newEquipmentNo = String.format(equipmentType + "%05d", newEquipment);
		}

		return newEquipmentNo;
	}

	/**
	 * 批量生成代码
	 */
	@RequiresPermissions("tool:gen:code")
	@Log(title = "代码生成", businessType = BusinessType.GENCODE)
	@GetMapping("/batchGenCode")
	@ResponseBody
	public void batchGenCode(HttpServletResponse response, String tables) throws IOException {
		String[] tableNames = Convert.toStrArray(tables);
	}

	@PostMapping("/showFieldRoleListBySysId")
	@ResponseBody
	public TableDataInfo showFieldRoleListBySysId(VerifyFieldRuleManage verifyFieldRuleManage) {
		VerifyFieldRuleManage queryParams = new VerifyFieldRuleManage();
		queryParams.setSysId(verifyFieldRuleManage.getSysId());
		startPage();
		List<VerifyFieldRuleManage> list = verifyFieldRuleManageService.selectVerifyFieldRuleManageList(queryParams);
		return getDataTable(list);
	}

	@RequiresPermissions("verify:task:showFieldPage")
	@GetMapping("/showFieldPage/{sysId}")
	public String showFieldPage(@PathVariable("sysId") Long sysId, ModelMap mmap) {
		mmap.put("sysId", sysId);
		return prefix + "/rule-table-list";
	}

	@RequiresPermissions("verify:task:detail")
	@GetMapping("/detail/{taskId}")
	public String detail(@PathVariable("taskId") Long taskId, ModelMap mmap) {
		VerifyTaskManage  verifyTaskManage  = verifyTaskManageService.selectVerifyTaskManageById(taskId);
		mmap.put("verifyTaskManage", verifyTaskManage);
		return prefix + "/task-detail";
	}
}
